#region Using Directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

#endregion

namespace Web
{
    /// <summary>
    /// Represents a list of items with a specified delimiter.
    /// Commonly used to make comma seperated lists of things.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Serializable]
    public sealed class DelimitedList<T> : List<T>, IEquatable<DelimitedList<T>>
    {
        #region Fields

        private string seperator;

        #endregion

        #region Constructors

        /// <summary>
        /// Create an empty list with a comma (,) as the seperator.
        /// </summary>
        public DelimitedList()
            : this(",")
        {
        }

        /// <summary>
        /// Create an empty list with the specified seperator.
        /// </summary>
        /// <param name="seperator"></param>
        public DelimitedList(string seperator)
            : this(seperator, null)
        {
        }

        public DelimitedList(params T[] initialList)
            : this(",", initialList)
        {
        }

        /// <summary>
        /// Create a new list populated with the given data and seperated by a comma (,).
        /// </summary>
        /// <param name="initialList"></param>
        public DelimitedList(IEnumerable<T> initialList)
            : this(",", initialList)
        {
        }

        /// <summary>
        /// Create a new list using the given seperator and populated with the given data.
        /// </summary>
        /// <param name="seperator">The string used to seperate items in the list</param>
        /// <param name="initialList">The initial list of data seperated by the seperator specified</param>
        public DelimitedList(string seperator, IEnumerable<T> initialList)
        {
            this.seperator = seperator;
            if (initialList != null)
            {
                AddRange(initialList);
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// An read only collection of the items in the list
        /// </summary>
        public ReadOnlyCollection<T> Items
        {
            get
            {
                return Array.AsReadOnly<T>(ToArray());
            }
        }

        private string[] StringItems
        {
            get
            {
                List<string> items = new List<string>();
                foreach (T item in Items)
                {
                    if (item != null)
                        items.Add(item.ToString());
                    else
                        items.Add(String.Empty);
                }
                return items.ToArray();
            }
        }

        /// <summary>
        /// The string to seperate the items in the list by
        /// </summary>
        public string Seperator
        {
            get { return seperator; }
            set { seperator = value; }
        }

        #endregion

        #region List Operators

        /// <summary>
        /// Finds the union of two lists.  The result list will
        /// contain all the items from both lists, with no repeat items.
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public DelimitedList<T> Union(DelimitedList<T> list)
        {
            DelimitedList<T> results = new DelimitedList<T>();
            results.Seperator = this.Seperator;

            foreach (T item in Items)
            {
                if (!results.Contains(item))
                {
                    results.Add(item);
                }
            }

            foreach (T item in list.Items)
            {
                if (!results.Contains(item))
                {
                    results.Add(item);
                }
            }

            return results;
        }

        /// <summary>
        /// Finds the Intersection of two lists.  The result list will
        /// contain all of the items that are common to both lists.
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public DelimitedList<T> Intersection(DelimitedList<T> list)
        {
            DelimitedList<T> results = new DelimitedList<T>();
            results.Seperator = this.Seperator;

            foreach (T item in Items)
            {
                if (list.Contains(item))
                {
                    results.Add(item);
                }
            }

            foreach (T item in list.Items)
            {
                if (this.Contains(item) && !results.Contains(item))
                {
                    results.Add(item);
                }
            }

            return results;
        }

        /// <summary>
        /// Finds the Difference of two lists.  The result list will
        /// contain the items that the two lists do not have in common.
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public DelimitedList<T> Difference(DelimitedList<T> list)
        {
            DelimitedList<T> results = new DelimitedList<T>();
            results.Seperator = this.Seperator;

            foreach (T item in Items)
            {
                if (!list.Contains(item))
                {
                    results.Add(item);
                }
            }

            foreach (T item in list.Items)
            {
                if (!this.Contains(item))
                {
                    results.Add(item);
                }
            }

            return results;
        }


        /// <summary>
        /// Checks if the given list is a subset of this list.
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool HasSubset(DelimitedList<T> list)
        {
            foreach (T item in list)
            {
                if (!this.Contains(item))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Checks if this list is a subset of the given list.
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool IsSubset(DelimitedList<T> list)
        {
            foreach (T item in Items)
            {
                if (!list.Contains(item))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// Finds the complement of two lists.  The result list will
        /// contain all of the items in list a that are not in list b.
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public DelimitedList<T> Complement(DelimitedList<T> list)
        {
            DelimitedList<T> results = new DelimitedList<T>();
            results.Seperator = this.Seperator;
            foreach (T item in Items)
            {
                if (!list.Contains(item))
                {
                    results.Add(item);
                }
            }
            return results;
        }

        #endregion

        #region Static Methods

        public static DelimitedList<string> Parse(string initialList)
        {
            return Parse(initialList, ",");
        }

        public static DelimitedList<string> Parse(string initialList, string seperator)
        {
            return new DelimitedList<string>(seperator, initialList.Split(new string[] { seperator }, StringSplitOptions.None));
        }

        #endregion

        #region ToString

        /// <summary>
        /// A list of the items in the list seperated by the current seperator
        /// </summary>
        public override string ToString()
        {
            if (seperator.Length == 0)
                return String.Empty;
            return String.Join(seperator, StringItems);
        }

        #endregion

        #region Operator Overloading

        public static bool operator ==(DelimitedList<T> list1, DelimitedList<T> list2)
        {
            if (Object.Equals(list1, null) && Object.Equals(list2, null))
                return true;
            if (Object.Equals(list1, null))
                return false;
            if (Object.Equals(list2, null))
                return false;
            return list1.Equals(list2);
        }

        public static bool operator !=(DelimitedList<T> list1, DelimitedList<T> list2)
        {
            if (Object.Equals(list1, null) && Object.Equals(list2, null))
                return false;
            if (Object.Equals(list1, null))
                return true;
            if (Object.Equals(list2, null))
                return true;
            return !list1.Equals(list2);
        }

        public override bool Equals(object obj)
        {
            if (Object.Equals(obj, null) || !(obj is DelimitedList<T>))
                return false;
            return Equals(obj as DelimitedList<T>);
        }

        public override int GetHashCode()
        {
            return ToString().GetHashCode();
        }

        #endregion

        #region IEquatable<DelimitedList<T>> Members

        public bool Equals(DelimitedList<T> other)
        {
            if (Object.Equals(other, null))
                return false;
            if (Count != other.Count)
                return false;
            for (int i = 0; i < Count; i++)
            {
                if (!Object.Equals(this[i], other[i]))
                    return false;
            }
            return true;
        }

        #endregion
    }
}